package com.insectstudio.haiivloffical;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CommentsActivity extends Activity implements OnClickListener {
	
	private TextView txtTitle;
	private WebView wvComments;
	private LinearLayout btnBack;
	
	@SuppressLint("SetJavaScriptEnabled")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_comments);
		// get references
		txtTitle = (TextView) findViewById(R.id.txt_title);
		wvComments = (WebView) findViewById(R.id.wv_comments);
		btnBack = (LinearLayout) findViewById(R.id.btn_back);
		// set title
		txtTitle.setText(getString(R.string.Comments));
		btnBack.setVisibility(View.VISIBLE);
		btnBack.setOnClickListener(this);
		// get extra
		String commentsUrl = getIntent().getStringExtra("commentsUrl");
		// get screen width
//		Display display = getWindowManager().getDefaultDisplay();
//		@SuppressWarnings("deprecation")
//		int width = display.getWidth();
//		commentsUrl = commentsUrl.replace("[width]", String.valueOf(width));
		// setting for web view
		wvComments.setWebChromeClient(new MyWebChromeClient());
		wvComments.setWebViewClient(new MyWebViewClient());
		wvComments.clearCache(true);
		wvComments.clearHistory();
		wvComments.getSettings().setJavaScriptEnabled(true);
		wvComments.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		// load url
		wvComments.loadUrl(commentsUrl);
	}
	
	class MyWebViewClient extends WebViewClient {
		
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			// TODO Auto-generated method stub
			view.loadUrl(url);
			return true;
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			// TODO Auto-generated method stub
			super.onPageFinished(view, url);
		}
		
		@Override
		public void onReceivedSslError(WebView view, SslErrorHandler handler,
				SslError error) {
			// TODO Auto-generated method stub
//			super.onReceivedSslError(view, handler, error);
			handler.proceed();
		}
	}
	
	class MyWebChromeClient extends WebChromeClient {
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_back:
			onBackPressed();
			break;
		}
		
	}

}
